﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace CommHelper
{

    
    public class DataToInfo
    {
        private DataTable _table = new DataTable();

        public DataTable table
        {
            set { _table = value; }
            get { return _table; }
        }
    }

    public class DataToList
    {
        public List<DataToInfo> tables = new List<DataToInfo>();
    }


    public class JSONManager
    {
        public static List<Dictionary<string, string>> ListJsonParse(string JsonString)
        {

            
            List<Dictionary<string, string>> Dics = new List<Dictionary<string, string>>();
            List<object> os;
            try
            {
                os = JsonConvert.DeserializeObject<List<object>>(JsonString.Replace("\\", "\\\\"));
            }
            catch
            {
                os = JsonConvert.DeserializeObject<List<object>>(JsonString);
            }
            foreach (object s in os)
            {
                Dics.Add(SimpleJsonParse(s.ToString()));
            }
            return Dics;
        }

        public static Dictionary<string, string> SimpleJsonParse(string Json)
        {
            JObject jsonObj = JObject.Parse(Json);
            IEnumerator enumerator = jsonObj.Children().GetEnumerator();
            Dictionary<string, string> DataDes = new Dictionary<string, string>();
            //Int32 ItemId = 0;
            while (enumerator.MoveNext())
            {
                string name = ((JProperty)((JToken)enumerator.Current)).Name;
                string value = GetJsonValue(jsonObj.Children(), name);
                //if (name.ToLower() == "id")
                //{
                //    ItemId = Int32.Parse(value);
                //    continue;
                //}
                DataDes.Add(name, value);
            }
            return DataDes;
        }

        public static DataToList DataSetParse(DataSet ds)
        {
            DataToList tableList = new DataToList();
            if (ds == null || ds.Tables.Count == 0)
            {
                return tableList;
            }

            for (var i = 0; i < ds.Tables.Count; i++)
            {
                tableList.tables.Add(new DataToInfo()
                {
                    table = ds.Tables[i],
                });
            }
            
            return tableList;
        }

        public static DataToInfo DataTableParse(DataTable dt)
        {
            DataToInfo dataInfo = new DataToInfo();
            if (dt == null || dt.Rows.Count == 0)
            {
                return dataInfo;
            }

            dataInfo.table = dt;
            return dataInfo;
        }

        private static string GetJsonValue(JEnumerable<JToken> jToken, string key)
        {
            IEnumerator enumerator = jToken.GetEnumerator();
            while (enumerator.MoveNext())
            {
                JToken jc = (JToken)enumerator.Current;

                if (jc is JObject || ((JProperty)jc).Value is JObject)
                {
                    return GetJsonValue(jc.Children(), key);
                }
                else
                {
                    if (((JProperty)jc).Name == key)
                    {
                        // Console.WriteLine(key + ":" + ((JProperty)jc).Value.ToString());
                        return ((JProperty)jc).Value.ToString();
                    }
                }
            }
            return null;
        }
    }
}
